
exports.get = function(player, req, resp, onHandled) {
    do{
        var user = player.user;
           
        // 没加入军团
        if( !user.league.name ) {
            resp.code = 1; resp.desc = 'not in any league'; break;
        }
        
        var nowTime = common.getTime();
        var today = new Date();

        var todayStartTime = +(new Date(today.getFullYear(), today.getMonth(), today.getDate()))/1000;
        var resetTime = todayStartTime + gConfGlobal.LeagueBossResetHour*3600;
        var startTime = todayStartTime + gConfGlobal.LeagueBossStartHour*3600 - 10;
        var endTime =  todayStartTime + gConfGlobal.LeagueBossEndHour*3600 + 10;

        // 时间限制(重置到开始期间不能发请求)
        if( nowTime < startTime && nowTime > resetTime ) {
            resp.code = 1; resp.desc = 'wrong time to get'; break;
        }
        
    }while(false);

    if( resp.code == 1 ) {
        onHandled();
        return;
    }

    req.args.league_name = user.league.name;       
    req.args.join = user.boss.join;
    req.args.got_reward = user.boss.got_reward;

    requestWorld(req, resp, function() {
        if( resp.code == 0 ) {
            if( resp.data.bossid && resp.data.bossid != user.boss.join ) {
                // 重置boss
                user.boss.join = resp.data.bossid;
                user.boss.got_reward = 0;
                user.boss.time = 0;
                user.boss.inspire = 0;
                player.markDirty('boss');

                resp.data.reset = 1;
            }
        }

        onHandled();
    });
}

exports.rank = function(player, req, resp, onHandled) {
    var user = player.user;
    if( !user.league.name ) {
        // 没加入军团            
        resp.code = 1; resp.desc = 'not in any league'; 
        onHandled();
        return;
    }

    req.args.league_name = user.league.name;
    requestWorld(req, resp, onHandled);
}

exports.get_kill_reward = function(player, req, resp, onHandled) {
    var user = player.user;
    // 没加入军团            
    if( !user.league.name ) {
        resp.code = 1; resp.desc = 'not in any league';
        onHandled();
        return;
    }

    req.args.league_name = user.league.name;
    requestWorld(req, resp, function() {    
        if( resp.code == 0 ) {
            var awards = []; 
            var leagueBossRewardConf = gConfLeagueBossReward.kill;
            if( leagueBossRewardConf ) { 
                awards.push(leagueBossRewardConf.Award1);
                awards.push(leagueBossRewardConf.Award2);
            }

            resp.data.awards = player.addAwards(awards);
        }

        onHandled();
    });
}

exports.get_reward = function(player, req, resp, onHandled) {
    var user = player.user;
    // 没加入军团            
    if( !user.league.name ) {
        resp.code = 1; resp.desc = 'not in any league';
        onHandled();
        return;
    }

    var league_name = user.league.name; 
    req.args.league_name = league_name;
    req.args.got_reward = user.boss.got_reward;
    requestWorld(req, resp, function() {
        if( resp.code == 0 ) {
            user.boss.got_reward = resp.data.bossid;
            player.markDirty('boss.got_reward');

            resp.data.awards = player.addAwards(resp.data.awards);
        }

        onHandled();
    });
}

exports.fight = function(player, req, resp, onHandled) {
    var user = player.user;
    do {
        // 没加入军团
        if( !user.league.name ) {
            resp.code = 1; resp.desc = 'not in any league'; break;
        }
        
        if( (common.getTime() - user.boss.time) < (gConfGlobal.LeagueBossCoolTime-10) ) {
            resp.code = 1; resp.desc = 'in cool time'; break;
        }

        var nowTime = common.getTime();
        var today = new Date();

        var todayStartTime = +(new Date(today.getFullYear(), today.getMonth(), today.getDate()))/1000;
        var startTime = todayStartTime + gConfGlobal.LeagueBossStartHour*3600 - 10;
        var endTime =  todayStartTime + gConfGlobal.LeagueBossEndHour*3600 + 10;

        if( nowTime < startTime || nowTime > endTime ) {
            resp.code = 1; resp.desc = 'not in fight time'; break;
        }

    }while(false);

    if( resp.code == 1 ) {
        onHandled();
        return;
    }

    req.args.league_name = user.league.name;
    req.args.join = user.boss.join;
    requestWorld(req, resp, function() {
        if( resp.code == 0 && resp.data.time ) {
            user.boss.time = resp.data.time;
            player.markDirty('boss.time');
        }

        onHandled();
    });
}

exports.inspire = function(player, req, resp, onHandled ) {
    do {
        var type = req.args.type;
        if( type != 'cash' && type != 'honor' ) {
            resp.code = 1; resp.desc = 'invalid args'; break;
        }

        var user = player.user;
        // 没加入军团            
        if( !user.league.name ) {
            resp.code = 1; resp.desc = 'not in any league'; break;
        }

        // 是否超过上限
        var inspired = user.boss.inspire * gConfGlobal.LeagueBossInspireAttack;
        if( inspired >= gConfGlobal.LeagueBossInspireLimit ) {
            resp.code=1; resp.desc='upto inspire limit'; break;
        }

        if( type == 'cash' ) {
            var cashCost = gConfGlobal.LeagueBossInspireCash;
            if( cashCost > user.status.cash ) {
                resp.code = 1; resp.desc = 'not enough cash'; break;
            }
            
            player.costResource('cash', cashCost, user.boss.inspire);
            resp.data.cash = -cashCost;
        }else {
            var honorCost = gConfGlobal.LeagueBossInspireHonor;
            if( honorCost > user.status.honor ) {
                resp.code = 1; resp.desc = 'not enough honor'; break;
            }

            user.status.honor -= honorCost;
            player.markDirty('status.honor');

            resp.data.honor = -honorCost;
        }

        user.boss.inspire += 1;
        player.markDirty('boss.inspire');

    }while(false);

    onHandled();
}

exports.rebirth = function(player, req, resp, onHandled) {
    do {
        var user = player.user;
        // 没加入军团            
        if( !user.league.name ) {
            resp.code = 1; resp.desc = 'not in any league'; break;
        }

        var cashCost = gConfGlobal.LeagueBossEndCoolCash;
        if( cashCost > user.status.cash ) {
            resp.code = 1; resp.desc = 'not enough cash'; break;
        }

        user.boss.time = 0;
        player.markDirty('boss.time');
        
        player.costResource('cash', cashCost);
        resp.data.cash = -cashCost;

    }while(false);

    onHandled();
}

exports.boss_blood = function(player, req, resp, onHandled) {
    var user = player.user;
    // 没加入军团            
    if( !user.league.name ) {
        resp.code = 1; resp.desc = 'not in any league'; 
        onHandled();
        return;
    }

    req.args.league_name = user.league.name;
    req.args.join = user.boss.join;
    requestWorld(req, resp, onHandled);
}
